/**
 * @(#) ControlSimulatorSensor.java
 */

package pcsr.simulator.control;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Line2D;

public class ControlSimulatorSensor extends ControlSimulatorNestedComponent
{
	private double sensorValue;
	private double sensorRange;
	private double sensorAngle;
	
	private double sensorIntersectX = Double.MIN_VALUE;
	private double sensorIntersectY = Double.MIN_VALUE;
	
	public ControlSimulatorSensor(String ID, ControlSimulatorStructure chassis, double angle)
	{
		super(ID, chassis);
		sensorAngle = angle;
	}

	/**
	 * @param sensorValue the sensorValue to set
	 */
	void setSensorValue(double sensorValue) {
		this.sensorValue = sensorValue;
	}

	/**
	 * @return the sensorValue
	 */
	public double getSensorValue() {
		return sensorValue;
	}

	/**
	 * @param sensorRange the sensorRange to set
	 */
	void setSensorRange(double sensorRange) {
		this.sensorRange = sensorRange;
	}

	/**
	 * @return the sensorRange
	 */
	public double getSensorRange() {
		return sensorRange;
	}
	
	private double GetXComponent(double range, double angle) {
		return range * Math.cos(angle);
	}
	
	private double GetYComponent(double range, double angle) {
		return range * Math.sin(angle);
	}
	
	@Override
	Shape GetMyArea()
	{
		return new Line2D.Double(
				GetXPos(),
				GetYPos(),
				GetXPos()+ GetXComponent(sensorRange,  parent.getRobotAngle() + sensorAngle),
				GetYPos()+ -1*(GetYComponent(sensorRange, parent.getRobotAngle() + sensorAngle))
				);
	}

	/**
	 * @param sensorAngle the sensorAngle to set
	 */
	void setSensorAngle(double sensorAngle) {
		this.sensorAngle = sensorAngle;
	}

	/**
	 * @return the sensorAngle
	 */
	public double getSensorAngle() {
		return sensorAngle;
	}

	
	public void setSensorIntersect(double intersectX,double intersectY) {
		this.sensorIntersectX = Math.max(intersectX, this.sensorIntersectX);
		this.sensorIntersectY = Math.max(intersectY, this.sensorIntersectY);
		
		this.sensorValue = this.sensorIntersectX+this.sensorIntersectY;
	}

	
	public void resetSensorIntersect() {
		this.sensorIntersectX = Double.MIN_VALUE;
		this.sensorIntersectY = Double.MIN_VALUE;
		this.sensorValue = 0;
	}

	public void setSensorIntersect(Rectangle collissionArea) {
		/*if(collissionArea.width > 0 || collissionArea.height > 0)
		{*/
			double dx = this.parent.GetXPos() - collissionArea.x;
			double dy = this.parent.GetYPos() - collissionArea.y;
			this.sensorValue = this.sensorRange - Math.abs( Math.sqrt(dx*dx+dy*dy) );
		/*}
		else
		{
			this.sensorValue = 0;
		}*/
	}
}
